﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using SAKS.Models;
using SAKS.Services;

namespace SAKS.UnitTest.ServicesTest
{
    [TestFixture]
    public class UserServicesTest : TestBase
    {
        [Test]
        public void GetUserProjectsTest()
        {
            var username = "GetUserProjectsTest";
            var user = new User
            {
                UserName = "GetUserProjectsTest",
                LoweredUserName = "GetUserProjectsTest",
                IsAnonymous = false,
                LastActivityDate = DateTime.Now,
                Projects = new List<Project>()
            };
            var project = new Project
            {
                Name = "GetUserProjectsTest",
                Users = new List<User>()
            };

            using (var session = sessionFactory.OpenSession())
            {
                project.Users.Add(user);
                user.Projects.Add(project);
                session.Save(project);
                session.Save(user);
                session.Flush();
                var userServices = new UserServices();
                userServices.Session = session;
                var projects = userServices.GetUserProjects(username);
                Assert.AreEqual(projects.Count,1);

            }
        }
        [Test]
        public void AddPersonInfoTest()
        {
            string UserName = "AddPersonInfoTest";
            var personInfo = new PersonInfo()
            {
                BirthDay = DateTime.Now,
                Gender = Gender.Male,
                Name = "AddPersonInfoTest",
            };
          
            using (var session = sessionFactory.OpenSession())
            {
                using (var tx = session.BeginTransaction())
                {
                    var UserServices = new UserServices();
                    UserServices.Session = session;
                    UserServices.UserName = UserName;
                    UserServices.AddPersonInfo(UserName, personInfo);
                    tx.Commit();
                }
            }
            using (var session = sessionFactory.OpenSession())
            {
                var info = session.QueryOver<PersonInfo>().Where(p => p.UserName == UserName).SingleOrDefault();
                Assert.NotNull(info);
            }
        }

        [Test]
        public void UpdatePersonInfoTest()
        {
            const string UserName = "UpdatePersonInfoTest";
            var personInfo = new PersonInfo()
            {
                BirthDay = DateTime.Now,
                Gender = Gender.Male,
                Name = "BeforeChanged",
            };
            using (var session = sessionFactory.OpenSession())
            {
                using (var tx = session.BeginTransaction())
                {
                    var UserServices = new UserServices();
                    UserServices.Session = session;
                    UserServices.AddPersonInfo(UserName,personInfo);
                    tx.Commit();
                }
            }
            using (var session = sessionFactory.OpenSession())
            {
                using (var tx = session.BeginTransaction())
                {
                    var newinfo = new PersonInfo()
                    {
                        BirthDay = DateTime.Now,
                        Gender = Gender.Male,
                        Name = "Changed",
                    };
                    var UserServices = new UserServices();
                    UserServices.Session = session;
                    var info = UserServices.GetPersonInfoByUserName(UserName);
                    Assert.AreEqual("BeforeChanged", info.Name);
                    UserServices.UpdatePersonInfo(newinfo);
                    tx.Commit();
                    var info1 = UserServices.GetPersonInfoByUserName(UserName);
                    Assert.AreEqual("Changed", info1.Name);
                }
            }

        }
    }
}
